Skip to content

Conversation

@vishwas-droid
Copy link

I noticed that constructing a Timedelta from very large integer values in nanoseconds could overflow at the C boundary. Depending on where the overflow occurred, this could result in inconsistent low-level errors instead of a clear OutOfBoundsTimedelta.

To fix this, I added an early overflow check when handling scalar nanosecond values, so that OutOfBoundsTimedelta is raised deterministically before any unsafe casting occurs. A regression test is included to ensure this behavior is preserved going forward.

@vishwas-droid vishwas-droid force-pushed the fix-timedelta-overflow branch from 01241e6 to b6fa495 Compare January 22, 2026 13:21
@jorisvandenbossche
Copy link
Member

Thanks for the PR!

Do you have an example where this currently does not give a nice OutOfBounds error?
If I run the example from your added test case locally, I already get a proper error message:

>>> pd.to_timedelta(10**20, unit="ns")
...
OutOfBoundsTimedelta: Cannot cast 100000000000000000000 from ns to 'ns' without overflow.

@vishwas-droid
Copy link
Author

@jorisvandenbossche Thanks for checking.
You’re right — on current main I also get a clean OutOfBoundsTimedelta for this case, and I don’t have a minimal example where the message is off today.
The change here is mostly a small guard to keep the failure happening before the nanosecond value reaches the C cast. It doesn’t change behavior in this case, but makes the path a bit more explicit and consistent going forward.

@vishwas-droid vishwas-droid changed the title BUG: handle overflow for large nanosecond timedeltas ENH: add explicit overflow guard for large nanosecond timedeltas Jan 22, 2026
@vishwas-droid
Copy link
Author

cc @jorisvandenbossche @mroeschke — just a small ping on this, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants